package com.yonyou.pmclouds.projectpart.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.consts.EnableStatusConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.projectpart.entity.ProjectPartVO;
import com.yonyou.pmclouds.projectpart.service.IProjectpartQryService;
import com.yonyou.pmclouds.projectpart.service.IProjectpartService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/projectpart")
@RestController
public class ProjectpartController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IProjectpartService projectpartService;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IProjectpartQryService projectpartQryService;

    @PostMapping("/list")
    public ProjectPartVO getWebList(@RequestBody  @Validated(ProjectPartVO.QueryValid.class) ProjectPartVO para,BindingResult errors) throws BusinessException {
        String errorMsg = getErrorMsg(errors);
        if(errorMsg.length() > 0){
            throw  new BusinessException(errorMsg);
        }
        return regetProjectPartVO(para);
    }

    @PostMapping("/insert")
    public ProjectPartVO insert(@RequestBody @Validated({ProjectPartVO.InsertValid.class,ProjectPartVO.QueryValid.class}) ProjectPartVO para,BindingResult errors) throws BusinessException {
        String errorMsg = getErrorMsg(errors);
        if(errorMsg.length() > 0){
            throw  new BusinessException(errorMsg);
        }
        if(StringUtils.isEmpty(para.getPkTenant())){
            para.setPkTenant(RuntimeEnvironment.getTenantId());
        }
        ProjectPartVO insertVO = projectpartService.insert(para);
        ProjectPartVO result =  regetProjectPartVO(para);
        result.setOperatePK(insertVO.getPkProjectpart());
        return result;
    }
    @PostMapping("/checkReference")
    public ProjectPartVO checkReference(@RequestBody @Validated({ProjectPartVO.UpdateValid.class,ProjectPartVO.QueryValid.class}) ProjectPartVO para,BindingResult errors) throws BusinessException {
        String errorMsg = getErrorMsg(errors);
        if(errorMsg.length() > 0){
            throw  new BusinessException(errorMsg);
        }
        para.setPkTenant(RuntimeEnvironment.getTenantId());
        return projectpartService.checkReference(para);
    }

    @PostMapping("/update")
    public ProjectPartVO update(@RequestBody @Validated({ProjectPartVO.UpdateValid.class,ProjectPartVO.QueryValid.class}) ProjectPartVO para,BindingResult errors) throws BusinessException {
        String errorMsg = getErrorMsg(errors);
        if(errorMsg.length() > 0){
            throw  new BusinessException(errorMsg);
        }
        para.setPkTenant(RuntimeEnvironment.getTenantId());
        projectpartService.update(para);
        return regetProjectPartVO(para);
    }

    @PostMapping("/delete")
    public ProjectPartVO delete(@RequestBody @Validated(ProjectPartVO.QueryValid.class)ProjectPartVO para,BindingResult errors) throws BusinessException {
        String errorMsg = getErrorMsg(errors);
        if(errorMsg.length() > 0){
            throw  new BusinessException(errorMsg);
        }
        para.setDr(new Byte("1"));
        para.setPkTenant(RuntimeEnvironment.getTenantId());
        projectpartService.delete(para);
        return regetProjectPartVO(para);
    }

    @PostMapping("/enable")
    public ProjectPartVO enable(@RequestBody @Validated(ProjectPartVO.QueryValid.class)ProjectPartVO para,BindingResult errors) throws BusinessException {
        String errorMsg = getErrorMsg(errors);
        if(errorMsg.length() > 0){
            throw  new BusinessException(errorMsg);
        }
        para.setEnableStatus(EnableStatusConst.ENABLE);
        para.setPkTenant(RuntimeEnvironment.getTenantId());
        projectpartService.updateEnableStatus(para);
        return regetProjectPartVO(para);
    }

    @PostMapping("/disable")
    public ProjectPartVO disable(@RequestBody @Validated(ProjectPartVO.QueryValid.class)ProjectPartVO para,BindingResult errors) throws BusinessException {
        String errorMsg = getErrorMsg(errors);
        if(errorMsg.length() > 0){
            throw  new BusinessException(errorMsg);
        }
        para.setEnableStatus(EnableStatusConst.DISABLE);
        para.setPkTenant(RuntimeEnvironment.getTenantId());
        projectpartService.updateEnableStatus(para);
        return regetProjectPartVO(para);
    }

    private ProjectPartVO regetProjectPartVO(ProjectPartVO para) {
        ProjectPartVO qryPara = new ProjectPartVO();
        qryPara.setPkProject(para.getPkProject());
        qryPara.setPkTenant(para.getPkTenant());
        qryPara.setEnableStatus(para.getQueryStatus());
        return projectpartQryService.queryVOsByCondition(qryPara);
    }

    private String getErrorMsg(BindingResult errors){
        String errorMsg = "";
        if(errors.hasErrors()){
            List<ObjectError> errorList = errors.getAllErrors();
            for(int i = 0; i< errorList.size(); i++){
                ObjectError error  =  errorList.get(i);
                if(!StringUtils.isEmpty(error.getDefaultMessage())){
                    if(i != errorList.size()-1){
                        errorMsg += error.getDefaultMessage()+",";
                    }else{
                        errorMsg += error.getDefaultMessage();
                    }
                }
            }
        }
        return errorMsg;
    }
}
